<template>
  <div>
    <form-render :form="form" @submit="submit"></form-render>
  </div>
</template>

<script>
import Vue from "vue";
import { Form, Toast } from "vant";

Vue.use(Form);
Vue.use(Toast);
// 表单渲染
import formRender from "../../components/form/formRender";
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations, mapActions } = createNamespacedHelpers(
  "enterpriseinfo"
);
// 企业表单 展示详情
export default {
  name: "enterpriseDetail",
  components: { formRender },
  data() {
    return {
      form: [],
      isModify: true,
      name: "" //是企业详情，还是企业用车部分,还是租赁合同
    };
  },
  created() {
    if (this.$route.params.form) {
      localStorage.setItem("form", JSON.stringify(this.$route.params));
    }
    this.form =
      this.$route.params.form || JSON.parse(localStorage.getItem("form")).form;
    this.isModify =
      this.$route.params.isModify ||
      JSON.parse(localStorage.getItem("form")).isModify;
    // this.name =
    //   this.$route.params.name || JSON.parse(localStorage.getItem("form")).name;
  },
  computed: {
    ...mapState({
      updateCorpStatus: state => state.updateCorpStatus
    })
  },
  methods: {
    ...mapActions(["actAddCorp", "actUpdateCorpData"]),
    ...mapMutations(["mutUpdateCorpStatus","mutDeleteid","mutClearDeleteid"]),
    deleteemit(val){
      console.log("val",val)
    },
    submit(val) {
      this.mutUpdateCorpStatus(val);
      if (this.isModify) {
        this.actUpdateCorpData()
          .then(res => {
            console.log(res)
            Toast(this.updateCorpStatus.text);
          })
          .catch(error => {
            Toast(this.updateCorpStatus.text);
          })
          // .then(res => {
          //   this.goBack();
          // });
      } else {
        this.actAddCorp()
          .then(res => {
             if (this.updateCorpStatus.code == 200) {
               Toast(this.updateCorpStatus.text);
               this.goBack();
             }else{
               Toast(this.updateCorpStatus.text);
             }
          })
          .catch(error => {
            Toast(this.updateCorpStatus.text);
          })
      }
    },
    goBack() {
      this.$router.go(-1);
    }
  }
};
</script>

<style lang="less" scoped>
</style>
